unit uSeason.local;

interface

uses uBase.local;

type
  TStandingItem = record
    Points,
    Index : integer;
  end;

  TStandingList = array of TStandingItem;

  TSeason = class( TObject )
  public
    DriverStandings : TStandingList;
    TeamStandings   : TStandingList;

    CurrentTrack : integer;

    constructor Create();
    destructor Destroy(); Override;
    procedure New();
    procedure SortStandings(var nArray:TStandingList);//Bubble Sort
  end;

implementation

uses uDebug.common, SysUtils, uGame.Local;

constructor TSeason.Create();
begin
  inherited Create;

  DevConsole.Msg(ES_DEBUG, '[TSeason.Create]');
end;
destructor TSeason.Destroy();
begin
  inherited Destroy;
end;

procedure TSeason.New();
var D :integer;
begin
  DevConsole.Msg(ES_DEBUG, '[TSeason.New]');

  DevConsole.Msg(ES_DEBUG, 'Drivers: '+IntToStr( High(Game.WorldBase.DriverList.Items )));

  if (High(DriverStandings) <= 0) then
    SetLength( DriverStandings, Game.WorldBase.DriverList.Count );

  for D := 0 to High( DriverStandings ) do
    with DriverStandings[D] do
      Index  := D;

  DevConsole.Msg(ES_DEBUG, 'Teams: '+IntToStr(Game.WorldBase.TeamList.Count));

  if (High(TeamStandings) <= 0) then
    SetLength( TeamStandings, Game.WorldBase.TeamList.Count );

  for D := 0 to High( TeamStandings ) do
    with TeamStandings[D] do
      Index  := D;

  CurrentTrack := 0; // Set to first track of the Season.
end;
procedure TSeason.SortStandings(var nArray:TStandingList);//Bubble Sort
// Sorts Drivers and Teams leader board.
var D :integer;
  Sorted : boolean;
  Temp : TStandingItem;
begin
  Sorted := TRUE;
  while Sorted do
  begin
    Sorted := FALSE;
    for D := 0 to High(nArray)-1 do
    begin
      if (nArray[D].Points < nArray[D+1].Points) then
      begin
        Temp := nArray[D+1];
        nArray[D+1] := nArray[D];
        nArray[D] := Temp;

        Sorted := TRUE;
      end;
    end;
  end;
end;

end.
